package com.xqls.biz.impl;



import com.xqls.biz.FluggasBlockAdministrationBiz;
import com.xqls.biz.FluggasDischargeRealdataRecordBiz;
import com.xqls.biz.FluggasTableFieldBiz;
import com.xqls.service.FluggasDischargeRealdataRecordService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hdg on 2017/7/14.
 */
@Service
public class FluggasDischargeRealdataRecordBizImpl implements FluggasDischargeRealdataRecordBiz {
    private static Logger logger = Logger.getLogger(FluggasDischargeRealdataRecordBiz.class);

    @Autowired
    private FluggasDischargeRealdataRecordService realdatarecordService;

    @Autowired
    private FluggasBlockAdministrationBiz FluggasBlockAdministrationBiz;

    @Autowired
    private FluggasTableFieldBiz FluggasTableFieldBiz;
    @Override
    public CommonResult queryFluggasDischargeRealdataRecord(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();

        try {
            List<String> columns_name = new ArrayList<String>();
            List<Object> columns = new ArrayList<Object>();
            List<String> displayfiled = FluggasTableFieldBiz.FieldEnglish(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns.add("viewId");
            for (String display : displayfiled) {
                if(!display.equals("realdata_no")){
                    columns.add(display);
                }

            }
            columns.add("recording_time");
            List<String> fieldalias =  FluggasTableFieldBiz.FieldChinese(FluggasBlockAdministrationBiz.BlockAdministration(commonPage.pageToMap()));
            columns_name.add("序号");
            for (String fieldalia : fieldalias) {
                if(!fieldalia.equals("编号")){
                    columns_name.add(fieldalia);
                }

            }
            columns_name.add("记录时间");
            List<Map<String, Object>> datas = realdatarecordService.selectByMap(commonPage.pageToMap());
            List<String> allstate= new ArrayList<>();
            for(int i=0;i<datas.size();i++){
                String state="";
                Iterator it=datas.get(i).keySet().iterator();
                while(it.hasNext()){
                    String key ="";
                    String value="";
                    key = it.next().toString();
                    for (String display : displayfiled) {
                        if (key.indexOf(display+"_state") >= 0) {
                            String start="";
                            if(("O2_content_state").equals(key)){
                                start+="O2含量设备";
                            }else if(("smoke_velocity_state").equals(key)){
                                start+="烟气流速设备";
                            }else if(("smoke_temperature_state").equals(key)){
                                start+="烟气温度设备";
                            }else if(("smoke_dynamic_pressure_state").equals(key)){
                                start+="烟气动压设备";
                            }else if(("smoke_humidity_state").equals(key)){
                                start+="烟气湿度设备";
                            }else if(("smoke_pressure_state").equals(key)){
                                start+="烟气压力设备";
                            }else if(("smoke_state").equals(key)){
                                start+="烟尘设备";
                            }else if(("SO2_state").equals(key)){
                                start+="二氧化硫设备";
                            }else if(("NO2_state").equals(key)){
                                start+="氮氧化物设备";
                            }else if(("CO_state").equals(key)){
                                start+="一氧化碳设备";
                            }else if(("H2S_state").equals(key)){
                                start+="硫化氢设备";
                            }else if(("HCl_state").equals(key)){
                                start+="氯化氢设备";
                            }else if(("NH3_state").equals(key)){
                                start+="氨设备";
                            }else if(("Cl2_state").equals(key)){
                                start+="氯气设备";
                            }else if(("CS2_state").equals(key)){
                                start+="二硫化碳设备";
                            }else if(("benzene_series_state").equals(key)){
                                start+="苯系物设备";
                            }else if(("toluene_state").equals(key)){
                                start+="甲苯设备";
                            }else if(("xylene_state").equals(key)){
                                start+="二甲苯设备";
                            }else if(("formaldehyde_state").equals(key)){
                                start+="甲醛设备";
                            }else if(("Phenols_state").equals(key)){
                                start+="酚类设备";
                            }else if(("methanol_state").equals(key)){
                                start+="甲醇设备";
                            }else if(("vinyl_chloride_state").equals(key)){
                                start+="氯乙烯设备";
                            }else if(("CO2_state").equals(key)){
                                start+="二氧化碳设备";
                            }
                            value = datas.get(i).get(key).toString();
                            if(("故障").equals(value)) {
                                state += start + ":" + value + ";";
                            }
                        }
                    }

                }
                allstate.add(state);
            }
            int curr=commonPage.getCurr();
            for (int i = 0;i<datas.size();i++){
                Date time = (Date) datas.get(i).get("recording_time");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(time);
                datas.get(i).put("recording_time",dateString);
                datas.get(i).put("viewId", i + 1+((curr-1)*15));
                datas.get(i).put("all_state",allstate.get(i));
            }
            commonPage.setPageData(datas);

            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("realdatarecordList", commonPage.getPageData());
            showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
                    columns.toArray(), null, "废气排放口实时数据历史记录"));
            commonPage.setHtml(VelocityUtil.getHtmlByMap("RealdataRecord/realdatarecordListData.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);

        } catch (Exception e) {
            logger.error("查询废气排放口实时数据历史记录异常！", e);
        }
        return result;
    }


    @Override
    public List<Map<String, Object>> queryAll() {
        return realdatarecordService.selectAll();
    }
}
